import { createRouter, createWebHistory } from 'vue-router'


const routes = [
  {
    path: '/',
    redirect: '/user'
  },
  {
    path: '/404',
    name: '404',
    component: () => import ("@/views/error/404.vue")
  },
  {
    path: '/login',
    name: 'login',
    component: () => import ("@/views/login/index.vue"),
  },
  {
    path: '/layout',
    name: 'layout',
    component: () => import ("@/views/layout/index.vue"),
    children: [
      {
        path: '/user',
        name: 'user',
        component: () => import ("@/views/user/index.vue")
      },
      {
        path: '/article',
        name: 'article',
        component: () => import ("@/views/article/index.vue")
      },
      {
        path: '/article/data',
        name: 'article_data',
        component: () => import ("@/views/article/data.vue")
      },
      {
        path: '/article/info',
        name: 'article_info',
        component: () => import ("@/views/article/info.vue")
      },
      {
        path: '/chatinfo',
        name: 'chatinfo',
        component: () => import ("@/views/chatinfo/index.vue")
      }
    ]
  },
]

const Router = createRouter({
  history: createWebHistory(import.meta.env.VITE_PUBLIC_PATH),
  routes: routes
})

let rootRoutes = []; // 路由列表
Router.beforeEach(async (to, from) => {
  if(!rootRoutes.length) {
    rootRoutes = Router.getRoutes();
  }
    
  // 判断页面是否存在
  if(!rootRoutes.find(item => item.path == to.path)) {
    return {
      path: '/404'
    }
  }
  return true;
})

export default Router